package top.mccat.raven.utils.handler;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import top.mccat.raven.factory.ConfigFactory;
import top.mccat.raven.item.MyItem;
import top.mccat.raven.item.StrengthItem;
import top.mccat.raven.menu.InfoMenu;
import top.mccat.raven.utils.utils.ColorUtils;
import top.mccat.raven.utils.utils.DefaultValue;

import java.util.ArrayList;
import java.util.List;

import static top.mccat.raven.utils.utils.DefaultValue.STRENGTHTITLE;

/**
 * @ClassName: CommanderHandler
 * @Description: TODO
 * @Author: Raven
 * @Date: 2021/7/7
 * @Version: 1.0
 */
public class CommanderHandler implements CommandExecutor {
    private JavaPlugin plugin;
    private StrengthCommandHandler sch;
    private StrengthItem myItem;
    private final String normalStone = "§a普通强化石",luckStone = "§b幸运强化石",
            safeStone = "§5§l保护强化石",successStone = "§c§l必定成功强化石";
    private final String normalLore = "§b可以用来强化武器，在5级后会掉级，20级后会破碎。",luckLore = "§5在20级以前不会掉级的强化石。",
            safeLore = "§c在20级以上强化可以保护武器不被破坏的强化石。",successLore = "§e§l极其稀有的强化石，强化装备一定会成功不会失败！";
    public CommanderHandler(JavaPlugin plugin){
        this.plugin = plugin;
        sch = new StrengthCommandHandler(plugin);
    }

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] sonCommand) {
        if(commandSender instanceof Player){
            Player sender = (Player) commandSender;
            sch.setSender(sender);
            if(sonCommand.length == 0){
                InfoMenu.outPutPluginInfo(sender);
            }else {
                myItem = new MyItem(sender);
                ItemStack stack = null;
                if(sonCommand[0].equals("normal") && sonCommand.length==1){
                    if((stack = hasStone(sender.getInventory(),getStone(Material.GOLD_INGOT,normalStone,normalLore)))!=null){
                        if(sch.canBeStrength(myItem) && myItem.getLevel(StrengthCommandHandler.Title)!=30) {
                            stack.setAmount(stack.getAmount() - 1);
                        }
                        sch.onStrengthItem(myItem,StrengthCommandHandler.Title,false,false,false,false);
                    }else {
                        sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c&l你没有足够的强化石！请检查自己的背包！"));
                    }
                }else if(sonCommand[0].equals("luck") && sonCommand.length==1){
                    if((stack = hasStone(sender.getInventory(),getStone(Material.PAPER,luckStone,luckLore)))!=null) {
                        if(sch.canBeStrength(myItem) && myItem.getLevel(StrengthCommandHandler.Title)!=30) {
                            stack.setAmount(stack.getAmount() - 1);
                        }
                        sch.onStrengthItem(myItem, StrengthCommandHandler.Title, true, false, false, false);
                    }else {
                        sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c&l你没有足够的幸运强化石！请检查自己的背包！"));
                    }
                }else if(sonCommand[0].equals("safe") && sonCommand.length==1){
                    if((stack = hasStone(sender.getInventory(),getStone(Material.GHAST_TEAR,safeStone,safeLore)))!=null) {
                        if(sch.canBeStrength(myItem) && myItem.getLevel(StrengthCommandHandler.Title)!=30) {
                            stack.setAmount(stack.getAmount() - 1);
                        }
                        sch.onStrengthItem(myItem,StrengthCommandHandler.Title,false,true,false,false);
                    }else {
                        sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c&l你没有足够的保护强化石！请检查自己的背包！"));
                    }
                }else if(sonCommand[0].equals("success") && sonCommand.length==1){
                    if((stack = hasStone(sender.getInventory(),getStone(Material.FIREWORK_STAR,successStone,successLore)))!=null) {
                        if(sch.canBeStrength(myItem) && myItem.getLevel(StrengthCommandHandler.Title)!=30) {
                            stack.setAmount(stack.getAmount() - 1);
                        }
                        sch.onStrengthItem(myItem,StrengthCommandHandler.Title,false,false,true,false);
                    }else {
                        sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c&l你没有足够的绝对成功强化石！请检查自己的背包！"));
                    }
                }else if(sonCommand[0].equals("admin") && sonCommand.length==1 && sender.hasPermission("strengthMX.admin")){
                    sch.onStrengthItem(myItem,StrengthCommandHandler.Title,false,false,true,true);
                }else if(sonCommand[0].equals("normalstone") && sender.hasPermission("strengthMX.admin")){
                    if(sonCommand.length<2){
                        sender.sendMessage("§a[StrengthPlus] §b请输入个数！");
                    }else {
                        if(sonCommand[1].matches("^[0-9_]+$")) {//判断是否为数字的正则表达式
                            giveStone(sender,getStone(Material.GOLD_INGOT,normalStone,normalLore),Integer.parseInt(sonCommand[1]));
                            sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&b已发送强化石！"));
                        }
                    }
                }else if(sonCommand[0].equals("luckstone") && sender.hasPermission("strengthMX.admin")){
                    if(sonCommand.length<2){
                        sender.sendMessage("§a[StrengthPlus] §b请输入个数！");
                    }else {
                        if(sonCommand[1].matches("^[0-9_]+$")) {//判断是否为数字的正则表达式
                            giveStone(sender,getStone(Material.PAPER,luckStone,luckLore),Integer.parseInt(sonCommand[1]));
                            sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&b已发送幸运强化石！"));
                        }
                    }
                }else if(sonCommand[0].equals("safestone") && sender.hasPermission("strengthMX.admin")){
                    if(sonCommand.length<2){
                        sender.sendMessage("§a[StrengthPlus] §b请输入个数！");
                    }else {
                        if(sonCommand[1].matches("^[0-9_]+$")) {//判断是否为数字的正则表达式
                            giveStone(sender,getStone(Material.GHAST_TEAR,safeStone,safeLore),Integer.parseInt(sonCommand[1]));
                            sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&b已发送保护强化石！"));
                        }
                    }
                }else if(sonCommand[0].equals("successstone") && sender.hasPermission("strengthMX.admin")){
                    if(sonCommand.length<2){
                        sender.sendMessage("§a[StrengthPlus] §b请输入个数！");
                    }else {
                        if(sonCommand[1].matches("^[0-9_]+$")) {//判断是否为数字的正则表达式
                            giveStone(sender,getStone(Material.FIREWORK_STAR,successStone,successLore),Integer.parseInt(sonCommand[1]));
                            sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&b已发送必定成功强化石！"));
                        }
                    }
                }else if(sonCommand[0].equals("reload") && sender.hasPermission("strengthMX.admin")){
                    plugin.reloadConfig();
                    sender.sendMessage(ColorUtils.getColorStr(ColorUtils.getColorStr(STRENGTHTITLE+"&b插件配置文件重载成功")));
                }else {
                    sender.sendMessage(STRENGTHTITLE+":§b请输入正确的指令！");
                }
            }
        }else{
            if(sonCommand[0].equals("reload")){
                plugin.reloadConfig();
                plugin.getLogger().info(ColorUtils.getColorStr("&b插件配置文件重载成功"));
            }else {
                plugin.getLogger().info("插件命令不能在控制台使用！");
            }
        }
        return false;
    }

    private boolean giveStone(Player player, ItemStack strengthItem,int amount){
        PlayerInventory inventory = player.getInventory();
        while (true){
            if(inventory.firstEmpty()<0 || inventory.firstEmpty()>35){
                player.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c请确定你的背包有空位置！"));
                return false;
            }else {
                if(amount>64){
                    strengthItem.setAmount(64);
                    player.getInventory().setItem(inventory.firstEmpty(),strengthItem);
                    amount-=64;
                }else {
                    strengthItem.setAmount(amount);
                    player.getInventory().setItem(inventory.firstEmpty(),strengthItem);
                    return true;
                }
            }
        }
    }

    private ItemStack hasStone(PlayerInventory inventory,ItemStack strengthStack){
        for(ItemStack stack : inventory){
            if(stack!=null){
                if(stack.getType() == strengthStack.getType()){
                    if(stack.getItemMeta().getDisplayName().equals(strengthStack.getItemMeta().getDisplayName())) {
                        if(stack.getItemMeta().getLore().get(0).equals(strengthStack.getItemMeta().getLore().get(0))){
                            return stack;
                        }
                    }
                }
            }
        }
        return null;
    }

    private ItemStack getStone(Material material, String name, String lore){
        ItemStack stack = new ItemStack(material);
        ItemMeta meta = stack.getItemMeta();
        List<String> itemLore = new ArrayList<>();
        itemLore.add(lore);
        meta.setLore(itemLore);
        meta.setDisplayName(name);
        stack.setItemMeta(meta);
        return stack;
    }

    public void reloadConfig(ConfigFactory factory){
        sch.readConfigExtra();
        sch.setFactory(factory);
    }

}
